<template>
	<coreshop-page title="物流信息" mode="left">
		<view class="p-25">
			<view class="item-box" v-if="state.list.length > 0">
				<view class="item" v-for="item,index in state.list" :key="index">
					<view class="title">{{item.context}}</view>
					<view class="time">{{item.time}}</view>
				</view>
			</view>
			<view v-else class="layout-empty-box">
				<coreshop-empty :mode="EmptyEnum.data" text="暂无物流轨迹"></coreshop-empty>
			</view>
		</view>
	</coreshop-page>
</template>
<script setup lang="ts">
	import { reactive, ref, nextTick } from "vue";
	import { onLoad } from '@dcloudio/uni-app';
	import { queryLogisticsByApi } from '@/core/api';
	import type { Response } from '@/core/models';
	import { handleShowToast } from '@/core/utils';
	import { EmptyEnum } from '@/core/enum';

	interface QueryParams {
		params : string;
	}

	const state = reactive<{
		code : string;
		no : string;
		add : string;
		mobile : string;
		list : Array<any>;
	}>({
		code: "",
		no: "",
		add: "",
		mobile: "",
		list: [],
	})

	onLoad((query : QueryParams) => {
		let data : Array<any> = decodeURIComponent(query.params).split('&');
		data.forEach((item) => {
			let key : string = item.split("=")[0];
			if (key == 'code') {
				state.code = item.split("=")[1]
			}
			if (key == 'no') {
				state.no = item.split("=")[1]
			}
			if (key == 'add') {
				state.add = item.split("=")[1]
			}
			if (key == 'mobile') {
				state.mobile = item.split("=")[1]
			}
		})
		if (!state.code || !state.no) {
			handleShowToast('缺少物流查询参数', 'none', () => {
				setTimeout(() => {
					uni.navigateBack({
						delta: 1
					});
				}, 1500)
			})
			return;
		}
		getLogisticsByApi();
	})

	const getLogisticsByApi = async () => {
		const logisticsByApi : Response<any> = await queryLogisticsByApi({
			code: state.code,
			no: state.no,
			mobile: state.mobile,
		});
		if (logisticsByApi.status) {
			state.list = logisticsByApi.data?.data;
		} else {
			handleShowToast(logisticsByApi.msg)
		}
	}
</script>
<style lang="scss" scoped>
	@import './expressDelivery.scss';
</style>